package com.cacho.s2b.lesson.engine;

import com.cacho.s2b.lesson.util.JsonUtil;
import com.cacho.s2b.lesson.vo.OperationType;
import com.cacho.s2b.lesson.vo.ValueOperation;
import com.cacho.s2b.lesson.vo.ValueStore;
import com.cacho.s2b.lesson.vo.ValueStores;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;

/**
 * @Description 接口请求和返回 json body处理
 * @Author LingWei
 * @date 2023/12/31
 **/
@Slf4j
public class JsonOperator {
    private final ValueOperation operation;
    private final String ARRAY_SEP = ", ";

    public JsonOperator(ValueOperation operation) {
        this.operation = operation;
    }

    // 处理json格式数据
    public String processJson(String jsonStr, String jsonPath) {
        String temp = jsonStr;
        OperationType opType = operation.getOperationType();
        switch (opType) {
            case DELETE:
                temp = JsonUtil.delete(jsonStr, jsonPath);
                break;
            case UPDATE:
                temp = updateStores(jsonStr, jsonPath);
                break;
            case PUT:
                String key = operation.getKey();
                jsonStr = JsonUtil.putByPath(jsonStr, jsonPath, key, null); // 先新增节点
                temp = updateStores(jsonStr, jsonPath + "." + key); // 然后对节点赋值
                break;
            case ADD:
                Object original = JsonUtil.readAsString(jsonStr, jsonPath);
                // 如果原来 json 里面没有, 直接添加
                if (original == null) {
                    temp = updateStores(jsonStr, jsonPath);
                } else {
                    String str = String.valueOf(original).trim();
                    String strStores = storesToString();
                    str = str.substring(0, str.length() - 1) + "," + strStores + "]";
                    temp = JsonUtil.setJsonByPath(jsonStr, jsonPath, str);
                } // 如果原来有值，需要添加
                break;
        }
        return temp;
    }

    // 去拿具体数据后更新 json body
    private String updateStores(String jsonStr, String jsonPath) {
        String temp = "";
        ValueStores valueStores = operation.getValueStores();
        // 如果是数组{}， 需要转换为json里的 list []
        if (valueStores.isArray()) {
            String arrString = "";
            if (valueStores.getValueStores().length == 0) {
                arrString = ARRAY_SEP;  // 如果没有值
            } else {
                for (ValueStore store : valueStores.getValueStores()) {
                    if (store.isNeedQuote()) {
                        arrString += "\"" + StringEscapeUtils.escapeJson(store.getValue()) + "\"" + ARRAY_SEP;
                    } else {
                        arrString += store.getValue() + ARRAY_SEP;
                    }
                }
            }  // 针对 json 是 列表格式 []
            temp = JsonUtil.setJsonByPath(jsonStr, jsonPath, "[" + arrString + "]");  // 列表格式
        } else {
            ValueStore store = valueStores.getValueStores()[0];
            if (store.isNeedQuote()) {
                temp = JsonUtil.setByPath(jsonStr, jsonPath, store.getValue());
            } else {
                temp = JsonUtil.setJsonByPath(jsonStr, jsonPath, store.getValue());
            }
        }
        log.info("更新{}后的json：{}", jsonPath, temp);
        return temp;
    }

    // 将 ValueStores 里面的值组装,返回一个string 值
    private String storesToString() {
        String temp = "";
        ValueStores valueStores = operation.getValueStores();
        // 如果是数组{}， 需要转换为json里的 list []
        if (valueStores.isArray()) {
            String arrString = "";
            if (valueStores.getValueStores().length == 0) {
                arrString = ARRAY_SEP;  // 如果没有值
            } else {
                for (ValueStore store : valueStores.getValueStores()) {
                    if (store.isNeedQuote()) {
                        arrString += "\"" + StringEscapeUtils.escapeJson(store.getValue()) + "\"" + ARRAY_SEP;
                    } else {
                        arrString += store.getValue() + ARRAY_SEP;
                    }
                }
            }
            temp = "[" + arrString + "]";
        } else {
            ValueStore store = valueStores.getValueStores()[0];
            temp = store.isNeedQuote() ? "\"" + StringEscapeUtils.escapeJson(store.getValue()) + "\"" : store.getValue();
        }
        return temp;
    }
}
